package com.jams.goodox.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jams.goodox.R;

/**
 * Created by jams on 2017/5/17.
 * ***
 * Tel: ***
 */

public class LRTextView extends LinearLayout {
    private TextView tvLeft;//左边文字
    private TextView tvRight;//右边文字

    public LRTextView(Context context) {
        super(context);
    }

    public LRTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.lf_textview, this, true);
        tvLeft = (TextView) findViewById(R.id.tv_lr_left);
        tvRight = (TextView) findViewById(R.id.tv_lr_right);
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.LRTextView);
        if (attributes == null) {
            return;
        }
        //左边文字
        if (attributes.hasValue(R.styleable.LRTextView_lr_left_text)) {
            tvLeft.setText(attributes.getString(R.styleable.LRTextView_lr_left_text));
        }
        if (attributes.hasValue(R.styleable.LRTextView_lr_left_text_color)) {
            tvLeft.setTextColor(attributes.getColor(R.styleable.LRTextView_lr_left_text_color, -1));
        }
        if (attributes.hasValue(R.styleable.LRTextView_lr_left_text_size)) {
            tvLeft.setTextSize(TypedValue.COMPLEX_UNIT_PX, attributes.getDimension(R.styleable.LRTextView_lr_left_text_size, 15));
        }
        if (attributes.hasValue(R.styleable.LRTextView_lr_left_text_width)) {
            tvLeft.setWidth((int) attributes.getDimension(R.styleable.LRTextView_lr_left_text_width, LinearLayout.LayoutParams.WRAP_CONTENT));
        }
        if (attributes.hasValue(R.styleable.LRTextView_lr_left_text_gravity)) {
            switch (attributes.getInt(R.styleable.LRTextView_lr_left_text_gravity, 1)) {
                //具体字段含义可以无视，请参考attrs里面LRTextView
                case 1:
                    tvLeft.setGravity(Gravity.CENTER_VERTICAL);
                    break;
                case 2:
                    tvLeft.setGravity(Gravity.TOP);
                    break;
                case 3:
                    tvLeft.setGravity(Gravity.BOTTOM);
                    break;
                default:
                    break;
            }
        }

        //右边文字
        if (attributes.hasValue(R.styleable.LRTextView_lr_right_text)) {
            tvRight.setText(attributes.getString(R.styleable.LRTextView_lr_right_text));
        }
            if (attributes.hasValue(R.styleable.LRTextView_lr_right_text_color)) {
                tvRight.setTextColor(attributes.getColor(R.styleable.LRTextView_lr_right_text_color, -1));
            }
            if (attributes.hasValue(R.styleable.LRTextView_lr_right_text_size)) {
                tvRight.setTextSize(TypedValue.COMPLEX_UNIT_PX, attributes.getDimension(R.styleable.LRTextView_lr_left_text_size, 15));
            }
            if (attributes.hasValue(R.styleable.LRTextView_lr_right_text_gravity)) {
                switch (attributes.getInt(R.styleable.LRTextView_lr_right_text_gravity, 1)) {
                    //具体字段含义可以无视，请参考attrs里面LRTextView
                    case 1:
                        tvRight.setGravity(Gravity.CENTER_VERTICAL);
                        break;
                    case 2:
                        tvRight.setGravity(Gravity.TOP);
                        break;
                    case 3:
                        tvRight.setGravity(Gravity.BOTTOM);
                        break;
                    case 4:
                        tvRight.setGravity(Gravity.END);
                        break;
                    case 5:
                        tvRight.setGravity(Gravity.END | Gravity.TOP);
                    case 6:
                        tvRight.setGravity(Gravity.END | Gravity.CENTER_VERTICAL);
                        break;
                    default:
                        break;
                }
            }
        attributes.recycle();
    }


    public void setTvRight(String string) {
        if (string == null) {
            string = "";
        }
        tvRight.setText(string);
    }

    public void setTvLeft(String string) {
        if (string == null) {
            string = "";
        }
        tvLeft.setText(string);
    }
}
